import 'package:flutter/material.dart';
import 'theme_type.dart';

class ThemeProvider with ChangeNotifier {
  ThemeType _themeType = ThemeType.system;

  // 浅色主题颜色（基础基准）
  static const Color lightPrimary = Colors.blue;
  static const Color lightSecondary = Colors.cyan;
  static const Color lightSurface = Colors.white;
  static const Color lightBackground = Color(0xFFF9F9F9);
  static const Color lightError = Colors.redAccent;
  static const Color lightBorder = Color(0xFFE0E0E0);
  static const Color lightTextPrimary = Color(0xFF212121);
  static const Color lightTextSecondary = Color(0xFF757575);
  static const Color lightShadow = Colors.black54;

  // 深色主题颜色
  static const Color darkPrimary = Colors.blueAccent;
  static const Color darkSecondary = Colors.cyanAccent;
  static const Color darkSurface = Color(0xFF303030);
  static const Color darkBackground = Color(0xFF121212);
  static const Color darkError = Colors.redAccent;
  static const Color darkBorder = Color(0xFF424242);
  static const Color darkTextPrimary = Color(0xFFEEEEEE);
  static const Color darkTextSecondary = Color(0xFFBDBDBD);

  // 自定义主题颜色（仅按钮/导航栏亮青色，其余复用浅色）
  static const Color customPrimary = Color(0xFF00AECC);
  static const Color customSurface = lightSurface;
  static const Color customBackground = lightBackground;
  static const Color customError = lightError;
  static const Color customBorder = lightBorder;
  static const Color customTextPrimary = lightTextPrimary;
  static const Color customTextSecondary = lightTextSecondary;
  static const Color customShadow = lightShadow;

  ThemeType get themeType => _themeType;

  // 浅色主题
  ThemeData get lightTheme => ThemeData(
    brightness: Brightness.light,
    colorScheme: ColorScheme.light(
      primary: lightPrimary,
      secondary: lightSecondary,
      surface: lightSurface,
      background: lightBackground,
      error: lightError,
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        backgroundColor: lightPrimary,
        foregroundColor: Colors.white,
        elevation: 4,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
    ),
    outlinedButtonTheme: OutlinedButtonThemeData(
      style: OutlinedButton.styleFrom(
        foregroundColor: lightPrimary,
        side: BorderSide(color: lightPrimary),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
    ),
    cardTheme: CardTheme(
      color: lightSurface,
      elevation: 5,
      shadowColor: lightShadow,
      margin: const EdgeInsets.symmetric(vertical: 8),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: BorderSide(
          color: lightBorder,
          width: 1,
          strokeAlign: BorderSide.strokeAlignOutside,
        ),
      ),
    ),
    textTheme: TextTheme(
      headlineMedium: TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.bold,
        color: lightTextPrimary,
      ),
      bodyLarge: TextStyle(fontSize: 16, color: lightTextPrimary),
      bodyMedium: TextStyle(fontSize: 14, color: lightTextSecondary),
    ),
    appBarTheme: AppBarTheme(
      backgroundColor: lightPrimary,
      foregroundColor: Colors.white,
      elevation: 4,
    ),
  );

  // 深色主题
  ThemeData get darkTheme => ThemeData(
    brightness: Brightness.dark,
    colorScheme: ColorScheme.dark(
      primary: darkPrimary,
      secondary: darkSecondary,
      surface: darkSurface,
      background: darkBackground,
      error: darkError,
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        backgroundColor: darkPrimary,
        foregroundColor: Colors.white,
        elevation: 4,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
    ),
    outlinedButtonTheme: OutlinedButtonThemeData(
      style: OutlinedButton.styleFrom(
        foregroundColor: darkPrimary,
        side: BorderSide(color: darkPrimary),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
    ),
    cardTheme: CardTheme(
      color: darkSurface,
      elevation: 4,
      shadowColor: Colors.black54,
      margin: const EdgeInsets.symmetric(vertical: 8),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: BorderSide(
          color: darkBorder,
          width: 1,
          strokeAlign: BorderSide.strokeAlignOutside,
        ),
      ),
    ),
    textTheme: TextTheme(
      headlineMedium: TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.bold,
        color: darkTextPrimary,
      ),
      bodyLarge: TextStyle(fontSize: 16, color: darkTextPrimary),
      bodyMedium: TextStyle(fontSize: 14, color: darkTextSecondary),
    ),
    appBarTheme: AppBarTheme(
      backgroundColor: darkSurface,
      foregroundColor: Colors.white,
      elevation: 4,
    ),
  );

  // 自定义主题
  ThemeData get customTheme => ThemeData(
    brightness: Brightness.light,
    colorScheme: ColorScheme.light(
      primary: customPrimary,
      secondary: customPrimary,
      surface: customSurface,
      background: customBackground,
      error: customError,
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        backgroundColor: customPrimary,
        foregroundColor: Colors.white,
        elevation: 4,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
    ),
    outlinedButtonTheme: OutlinedButtonThemeData(
      style: OutlinedButton.styleFrom(
        foregroundColor: customPrimary,
        side: BorderSide(color: customPrimary),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
    ),
    cardTheme: CardTheme(
      color: customSurface,
      elevation: 5,
      shadowColor: customShadow,
      margin: const EdgeInsets.symmetric(vertical: 8),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: BorderSide(
          color: customBorder,
          width: 1,
          strokeAlign: BorderSide.strokeAlignOutside,
        ),
      ),
    ),
    textTheme: TextTheme(
      headlineMedium: TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.bold,
        color: customTextPrimary,
      ),
      bodyLarge: TextStyle(fontSize: 16, color: customTextPrimary),
      bodyMedium: TextStyle(fontSize: 14, color: customTextSecondary),
    ),
    appBarTheme: AppBarTheme(
      backgroundColor: customPrimary,
      foregroundColor: Colors.white,
      elevation: 4,
    ),
  );

  // 获取当前主题
  ThemeData get currentTheme {
    switch (_themeType) {
      case ThemeType.light:
        return lightTheme;
      case ThemeType.dark:
        return darkTheme;
      case ThemeType.custom:
        return customTheme;
      case ThemeType.system:
      default:
        return lightTheme;
    }
  }

  // 获取主题模式
  ThemeMode get themeMode {
    switch (_themeType) {
      case ThemeType.system:
        return ThemeMode.system;
      case ThemeType.light:
        return ThemeMode.light;
      case ThemeType.dark:
        return ThemeMode.dark;
      case ThemeType.custom:
        return ThemeMode.light;
    }
  }

  // 处理系统主题
  ThemeData getSystemTheme(BuildContext context) {
    return MediaQuery.platformBrightnessOf(context) == Brightness.dark
        ? darkTheme
        : lightTheme;
  }

  // 设置主题类型
  void setThemeType(ThemeType type) {
    _themeType = type;
    notifyListeners();
  }

  // 快捷设置方法
  void setSystemMode() => setThemeType(ThemeType.system);
  void setLightMode() => setThemeType(ThemeType.light);
  void setDarkMode() => setThemeType(ThemeType.dark);
  void setCustomMode() => setThemeType(ThemeType.custom);
}